/*
 * TableModelZonas.java
 *
 * Created on 4 de febrero de 2007, 11:59 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ig.util;

import ig.metrica.riesgo.FactorRiesgoMatrizCorrelacionMetrica;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author jsanchez
 */
public class TableModelTasasInteres extends AbstractTableModel {
//    private Collection<FactorRiesgoMatrizCorrelacionMetrica> collection=null;
    private boolean[]   editables={true,true};
    private String[] columnNames = {
        "Tasa de Interes/Factor de Riesgo", "Cambio Esperado"
    };
    private Hashtable<Integer,FactorRiesgoMatrizCorrelacionMetrica> hashTable=new Hashtable<Integer,FactorRiesgoMatrizCorrelacionMetrica>();
    private FactorRiesgoMatrizCorrelacionMetrica tmp=null;
    /** Creates a new instance of TableModelZonas */
    int i=0;
    public TableModelTasasInteres(Collection<FactorRiesgoMatrizCorrelacionMetrica> collection) {
//        this.collection=collection;
        
        for (FactorRiesgoMatrizCorrelacionMetrica elem : collection) {
            hashTable.put(i,elem);
            i++;
        }
    }
    public String getColumnName(int col) {
        return columnNames[col];
    }
    public int getRowCount() {
        return hashTable.size();
    }
    public synchronized void newRow(Integer idBanda){
        FactorRiesgoMatrizCorrelacionMetrica f=new FactorRiesgoMatrizCorrelacionMetrica("",0.0,1,KeyGenerator.getInstance().getNewKey());
        f.setNuevo(true);
        hashTable.put(i++,f);
        fireTableDataChanged();
    }
    
    public synchronized void removeRow(Integer row){
        hashTable.remove(row);
        ordenar();
        fireTableDataChanged();
    }
    
    private void ordenar(){
        Hashtable<Integer,FactorRiesgoMatrizCorrelacionMetrica> clon=(Hashtable)hashTable.clone();
        Enumeration<FactorRiesgoMatrizCorrelacionMetrica> enumm=clon.elements();
        hashTable.clear();
        Integer ante=clon.size();
        i=0;
        while(enumm.hasMoreElements()){
            FactorRiesgoMatrizCorrelacionMetrica f=enumm.nextElement();
            hashTable.put(i,f);
            i++;
        }
        clon=(Hashtable)hashTable.clone();
        enumm=clon.elements();
        Integer j=0;
        hashTable.clear();
        while(enumm.hasMoreElements()){
            FactorRiesgoMatrizCorrelacionMetrica f=enumm.nextElement();
            hashTable.put(j,f);
            j++;
        }
    }
    
    public int getColumnCount() {
        return columnNames.length;
    }
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    
    public boolean isCellEditable(int row, int col) {
        return (editables[col]==true) ? true : false;
    }
    public Object getValueAt(int rowIndex, int columnIndex) {
        this.tmp=this.hashTable.get(rowIndex);
        if(columnIndex==0)
            return this.tmp.getNombre();
        else if(columnIndex==1)
            return this.tmp.getCambioEsperadoPBS().getCambioEsperado();
        return null;
    }
    
    public void setValueAt(Object value, int row, int col){
        this.tmp=this.hashTable.get(row);
        if(col==0)
            this.tmp.setNombre(value.toString());
        else if(col==1)
            this.tmp.getCambioEsperadoPBS().setCambioEsperado(Double.valueOf(value.toString()));
        this.fireTableCellUpdated(row,col);
    }
    public Collection<FactorRiesgoMatrizCorrelacionMetrica> getData(){
        Collection<FactorRiesgoMatrizCorrelacionMetrica> c=new ArrayList<FactorRiesgoMatrizCorrelacionMetrica>();
        Enumeration<FactorRiesgoMatrizCorrelacionMetrica> enumm=hashTable.elements();
        while (enumm.hasMoreElements()) {
            c.add(enumm.nextElement());
        }
        return c;
    }
    public Object getDTO(int rowIndex){
        return this.hashTable.get(rowIndex);
    }
    public void getDTO(int rowIndex,FactorRiesgoMatrizCorrelacionMetrica obj){
        this.hashTable.put(rowIndex,obj);
    }
    
    public void setData(Collection<FactorRiesgoMatrizCorrelacionMetrica> collection) {
        i=0;
        hashTable=new Hashtable<Integer,FactorRiesgoMatrizCorrelacionMetrica>();
        for (FactorRiesgoMatrizCorrelacionMetrica elem : collection) {
            hashTable.put(i,elem);
            i++;
        }
        fireTableDataChanged();
    }
}
